package my_collection;

import java.util.ArrayList;
import java.util.List;

public class MyMapTest {

	public static void main(String[] args) {
		MyMap<String, Integer> map = new MyMap<>();
		map.put("1", 1);
		map.put("2", 2);
		map.put("3", 3);
		map.put("1", 4);
		System.out.println(map.get("1"));
		//System.out.println(map.getAll());
		
	}

}

class MyMap<K, V>{
	private List<Node<K,V>> list;
	private int size;
	public MyMap() {
		list = new ArrayList<>();
		size = 0;
	}
	
	public void put(K key, V value) {
		boolean flag = true;
		for(int i=0;i<size;i++) {
			if(list.get(i).getKey().equals(key)) {
				list.get(i).setValue(value);
				flag = false;
			}
		}
		if(flag) {
			list.add(new Node<>(key, value));
			size++;
		}
		
	}
	public V get(K key) {
		for(Node<K,V> n : list) {
			if(n.getKey().equals(key)) {
				return n.getValue();
			}
		}
		return null;
	}
	
	public List<Node<K,V>> getAll(){
		return list;
	}
	
	static class Node<K, V>{
		final K key;
		V value;
		Node(K key, V value){
			this.key = key;
			this.value = value;
		}
		public V getValue() {
			return value;
		}
		public void setValue(V value) {
			this.value = value;
		}
		public K getKey() {
			return key;
		}
		@Override
		public String toString() {
			return "Node [key=" + key + ", value=" + value + "]";
		}
		
	}
}

